package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.ITripLoginLog;
import com.gec.model.system.ITripUser;
import com.gec.model.vo.ITripLoginLogQueryVo;
import com.gec.system.mapper.ITripLoginLogMapper;
import com.gec.system.mapper.ITripUserMapper;
import com.gec.system.service.ITripLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 系统访问记录 服务实现类
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-20
 */
@Service
@Transactional
public class ITripLoginLogServiceImpl extends ServiceImpl<ITripLoginLogMapper, ITripLoginLog> implements ITripLoginLogService {

    @Autowired
    private ITripLoginLogMapper iTripLoginLogMapper;

    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        ITripLoginLog iTripLoginLog = new
                ITripLoginLog();
        iTripLoginLog.setUsername(username);
        iTripLoginLog.setStatus(status);
        iTripLoginLog.setIpaddr(ipaddr);
        iTripLoginLog.setMsg(message);

        this.iTripLoginLogMapper.insert(iTripLoginLog);
    }

    @Override
    public IPage<ITripLoginLog> selectPage(long page, long limit, ITripLoginLogQueryVo iTripLoginLogQueryVo) {
        //创建page对象
        Page<ITripLoginLog> pageParam = new Page(page,limit);
        //获取条件值
        String username = iTripLoginLogQueryVo.getUsername();
        String createTimeBegin = iTripLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = iTripLoginLogQueryVo.getCreateTimeEnd();
        //封装条件
        QueryWrapper<ITripLoginLog> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            wrapper.like("username",username);
        }
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.le("create_time",createTimeEnd);
        }
        //调用mapper方法
        IPage<ITripLoginLog> pageModel = iTripLoginLogMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    @Override
    public ITripLoginLog getById(Long id) {
        return iTripLoginLogMapper.selectById(id);
    }

    @Override
    public void removeById(Long id) {
        this.iTripLoginLogMapper.deleteById(id);
    }
}
